package com.yingxin.yxerp.module.inform.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yxerp.module.inform.entity.InformerSet;
import com.yingxin.yxerp.module.inform.mapper.InformerSetMapper;
import com.yingxin.yxerp.module.inform.service.IInformerSetService;
import com.yingxin.yxerp.module.inform.vo.req.InformerSetQueryVo;
import com.yingxin.yxerp.module.inform.vo.resp.InformerSetRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 通知人设置 服务实现类
 * </p>
 *
 * @author dongpengfei
 * @since 2021-12-10
 */
@Service
public class InformerSetServiceImpl extends ServiceImpl<InformerSetMapper, InformerSet> implements IInformerSetService {

    /**
     * 分页查询
     *
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<InformerSetRespVo> pageVo(Page page, InformerSetQueryVo queryVo) {
        IPage<InformerSetRespVo> voPage = baseMapper.pageVo(page, queryVo);
        List<InformerSetRespVo> voList = voPage.getRecords();
        for (InformerSetRespVo vo : voList) {
            getPerson(vo);
        }
        return voPage;
    }

    /**
     * 查询全部
     *
     * @param queryVo
     * @return
     */
    @Override
    public List<InformerSetRespVo> listVo(InformerSetQueryVo queryVo) {
        List<InformerSetRespVo> voList = baseMapper.listVo(queryVo);
        for (InformerSetRespVo vo : voList) {
            getPerson(vo);
        }
        return voList;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public InformerSetRespVo getVo(String id) {
        InformerSetRespVo vo = baseMapper.getVo(id);
        getPerson(vo);
        return vo;
    }

    /**
     * 通知人名赋值
     *
     * @param vo
     */
    private void getPerson(InformerSetRespVo vo) {
        List<String> informerNameList = new ArrayList<>();
        if (vo != null) {
            String informerIdList = vo.getInformerIdList();
            List<String> informerIds = JSONObject.parseArray(informerIdList, String.class);
            vo.setInformerIds(informerIds);
            for (String informerId : informerIds) {
                String informerName = baseMapper.getPerson(informerId);
                informerNameList.add(informerName);
            }
        }
        vo.setInformerNameList(informerNameList);
    }

    /**
     * 创建
     *
     * @param InformerSet
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(InformerSet InformerSet) {
        save(InformerSet);
    }


    @Override
    public List<String> getInformerIdList(String type) {
        String jsonList = baseMapper.getInformerIdList(type);
        return JSONObject.parseArray(jsonList, String.class);
    }

    @Override
    public int countByTypeName(String id, String informType) {
        QueryWrapper<InformerSet> qw = new QueryWrapper<>();
        qw.lambda().eq(InformerSet::getInformType, informType);
        if (StringUtils.isNotBlank(id)) {
            qw.lambda().ne(InformerSet::getId, id);
        }
        return count(qw);
    }
}
